<?php

class Buzz_AuthChecker {

    public function ownsElement($eid) {
        $result = false;
        $user = Zend_Auth::getInstance()->getIdentity();

        if ($eid instanceof Application_Model_Element) {
            $elem = $eid;
        } else {
            $emapper = new Application_Model_ElementMapper();
            $elem = $emapper->find($eid);
        }

        if ($elem != null) {
            $result = $elem->owner_id() == $user->id() || $user->isAdmin();
        }

        return $result;
    }

    public function ownsGroup($gid) {
        $result = false;
        $user = Zend_Auth::getInstance()->getIdentity();

        if ($gid instanceof Application_Model_Group) {
            $group = $gid;
        } else {
            $gmapper = new Application_Model_GroupMapper();
            $group = $gmapper->find($gid);
        }

        if ($group != null) {
            $result = (($user->id() == $group->owner_id()) ||
                    (!$group->categorizing() && $user->isAdmin()) ||
                    $user->isSuperAdmin());
        }
        return $result;
    }

}
